import axios from 'axios'
import {Toast} from 'mint-ui'
//响应拦截器
axios.interceptors.response.use(success=>{
    if(success.status&&success.status==200){//服务器状态200表示请求成功
        if(success.data.message){
            Toast({message: success.data.message,position: 'bottom',duration: 5000});
        }
    }
    return success.data;
},error=>{
    console.log(error);
    if(error.response.code==404){
        Toast({message: '404 Error',position: 'bottom',duration: 5000});
    }
    return;
});
//请求拦截器
// axios.interceptors.request.use(config=>{
//     //如果存在token，请求携带这个token
//     if(window.sessionStorage.getItem('tokenStr')){
//         config.headers['Authorization']=window.sessionStorage.getItem('tokenStr');
//     }
//     return config;
// },error=>{});

let base=''
// let base='https://www.fastmock.site/mock/46fa8a14a29569df942481df826ee45f/api'
//若发给后端，这部分base为空字符串
//传送json格式的post请求
export const postRequest=(url,params,token)=>{
    if(token!=null){
        return axios({
            method:'post',
            url:`${base}${url}`,
            data:params,
            headers:{
                Authorization:token,
            }
        })
    }else return axios({
        method:'post',
        url:`${base}${url}`,
        data:params,
    })
}

//传送json格式的get请求
export const getRequest=(url,params,token)=>{
    if(token!=null){
        return axios({
            method:'get',
            url:`${base}${url}`,
            params:params,
            headers:{
                Authorization:token,
            }
        })
    }else return axios({
        method:'get',
        url:`${base}${url}`,
        params:params,
    })
}